package com.xiaoyu.tool.controller.admin;

import com.xiaoyu.tool.entity.adminUser.AdminUser;
import com.xiaoyu.tool.service.admin.AdminUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * session缓存操作
 * */
public abstract class AdminBaseController {

    public static final String ADMIN_SESSION_ID = "admin_session_id";

    private final HttpServletRequest servletRequest;

    private final AdminUserService adminUserService;

    protected AdminBaseController(HttpServletRequest servletRequest, AdminUserService adminUserService) {
        this.servletRequest = servletRequest;
        this.adminUserService = adminUserService;
    }

    void loginAdminUser(AdminUser adminUser){
        HttpSession session = servletRequest.getSession();
        session.setAttribute(ADMIN_SESSION_ID,adminUser.getId());
    }

    void logoutAdminUser(){
        HttpSession session = servletRequest.getSession();
        session.setAttribute(ADMIN_SESSION_ID,null);
    }

    AdminUser getAdminUser() {
        HttpSession session = servletRequest.getSession();
        Long adminUserId = (Long) session.getAttribute(ADMIN_SESSION_ID);
        return adminUserService.findAdminUserById(adminUserId).get();
    }

}
